package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private IStatementService statementService;




    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {


        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {

        //选择第二个
        statementItemMapper.updateByPrimaryKey(statementItem);

    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public void saveItem(List<StatementItem> items) {
        //1:保存结算单明细
        if(items != null && items.size() > 0){
            BigDecimal total = new BigDecimal("0.00");   //总数量
            BigDecimal amount = new BigDecimal("0.00");  //总金额
            //最后一个: 页面约定: 最后一个保存优惠价格
            StatementItem item = items.remove(items.size() - 1);
            //保存前先删除之前结算单明细
            statementItemMapper.deleteByStatementId(item.getStatementId());
            if(items.size() > 0){
                //保存结算单明细
                for (StatementItem statementItem : items) {
                    statementItemMapper.insert(statementItem);
                    //总数量 = 每个明细数量和
                    total = total.add(new BigDecimal(statementItem.getItemQuantity()));
                    //总金额 = (每个明细单价 * 数量) 之和
                    BigDecimal sum = statementItem.getItemPrice().multiply(new BigDecimal(statementItem.getItemQuantity()));
                    amount = amount.add(sum);

                }
                //2:更新结算单--总数量, 总金额, 优惠价
                statementService.updateAmount(item.getStatementId(),total, amount, item.getItemPrice());
            }else{
                //2:更新结算单--总数量, 总金额, 优惠价
                statementService.updateAmount(item.getStatementId(),total, amount, new BigDecimal("0.00"));
            }
        }
        //2:更新结算单--总数量, 总金额, 优惠价
        //总数量 = 每个明细数量和
        //总金额 = (每个明细单价 * 数量) 之和
















    }
}
